package com.itany.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.DictConstant;
import com.itany.cms.constant.StatusConstant;
import com.itany.cms.dao.CourseTypeMapper;
import com.itany.cms.entity.CourseType;
import com.itany.cms.exception.COurseTypeStatusErrorException;
import com.itany.cms.exception.CourseTypeExistException;
import com.itany.cms.exception.CourseTypeNotExistsException;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.CourseTypeService;
import com.itany.cms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CourseTypeServiceImpl implements CourseTypeService {
    @Autowired
    private CourseTypeMapper courseTypeMapper;

    @Override
    public void addFatherType(String typeName) throws RequestParameterErrorException, CourseTypeExistException {
        if (ParameterUtil.isNull(typeName)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type_name", typeName);
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        if (!types.isEmpty()) {
            throw new CourseTypeExistException("该课程类型已经存在");
        }
        CourseType type = new CourseType();
        type.setTypeName(typeName);
        type.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        courseTypeMapper.insert(type);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<CourseType> findByParams(CourseType courseTypeParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COURSE_TYPE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_TYPE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<CourseType> wrapper = new QueryWrapper<>();

        wrapper.isNull("parent_id");

        List<CourseType> courseTypes = courseTypeMapper.selectList(wrapper);
        PageInfo<CourseType> typePage = new PageInfo<>(courseTypes);
        return typePage;
    }

    @Override
    public void addSonType(String parentId, String typeName) throws RequestParameterErrorException, CourseTypeExistException {
        if (ParameterUtil.isNull(parentId) || ParameterUtil.isNull(typeName)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type_name", typeName);
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        if (!types.isEmpty()) {
            throw new CourseTypeExistException("该部门已经存在");
        }
        CourseType courseType = new CourseType();
        courseType.setTypeName(typeName);
        courseType.setParentId(Integer.parseInt(parentId));
        courseType.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        courseTypeMapper.insert(courseType);
    }

    @Override
    public void modifyName(String id, String typeName) throws RequestParameterErrorException, CourseTypeExistException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(typeName)) {

            throw new RequestParameterErrorException("请求参数有误");

        }
        QueryWrapper<CourseType> wrapper = new QueryWrapper();
        wrapper.eq("type_name", typeName)
                .eq("id", Integer.parseInt(id));
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        if (!types.isEmpty()) {
            throw new CourseTypeExistException("该课程类型已经存在");
        }
        CourseType type = new CourseType();
        type.setId(Integer.parseInt(id));
        type.setTypeName(typeName);
        courseTypeMapper.updateById(type);

    }

    @Override
    public void enabledType(String id) throws RequestParameterErrorException, COurseTypeStatusErrorException {
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseType courseType = courseTypeMapper.selectById(Integer.parseInt(id));
        CourseType fatherType = courseTypeMapper.selectById(courseType.getParentId());
        if (fatherType != null && fatherType.getStatus() == StatusConstant.COURSE_TYPE_STATUS_DISABLED) {
            throw new COurseTypeStatusErrorException("请先启用上级部门");
        }
        courseType.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        courseTypeMapper.updateById(courseType);
    }

    @Override
    public void disabledType(String id) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseType courseType = courseTypeMapper.selectById(Integer.parseInt(id));
        courseType.setStatus(StatusConstant.COURSE_TYPE_STATUS_DISABLED);
        courseTypeMapper.updateById(courseType);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("parent_id", courseType.getId());
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        for (CourseType sonType : types) {
            disabledType(sonType.getId().toString());
        }
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        CourseType courseType = new CourseType();
        courseType.setId(Integer.parseInt(id));
        courseType.setStatus(Integer.parseInt(status));
        if (courseType.getStatus() == StatusConstant.COURSE_TYPE_STATUS_ENABLED) {
            courseType.setStatus(StatusConstant.COURSE_TYPE_STATUS_DISABLED);
        } else {
            courseType.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        }
        courseTypeMapper.updateById(courseType);
    }

    @Override
    public List<CourseType> findEnabled() throws CourseTypeNotExistsException {
        QueryWrapper<CourseType> wrapper = new QueryWrapper<>();
        wrapper.eq("status", StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        if (types.isEmpty()) {
            throw new CourseTypeNotExistsException("当前没有可用的课程类型,请先启用");
        }
        return types;
    }

    @Override
    public PageInfo<CourseType> findFatherType(CourseType courseTypeParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COURSE_TYPE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_TYPE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<CourseType> wrapper = new QueryWrapper();
        wrapper.eq("id", courseTypeParam.getParentId());
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        Integer id = types.get(0).getParentId();
        QueryWrapper<CourseType> wrapper2 = new QueryWrapper<>();
        wrapper.eq("parent_id", id)
                .or()
                .isNull("parent_id");
        List<CourseType> types2 = courseTypeMapper.selectList(wrapper);
        PageInfo<CourseType> typePage = new PageInfo<>(types2);
        return typePage;

    }

    @Override
    public PageInfo<CourseType> findSonType(CourseType courseTypeParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.COURSE_TYPE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.COURSE_TYPE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<CourseType> wrapper = new QueryWrapper();
        wrapper.eq("parent_id", courseTypeParam.getId());
        List<CourseType> types = courseTypeMapper.selectList(wrapper);
        PageInfo<CourseType> typePage = new PageInfo<>(types);
        return typePage;
    }
}
